{% set isNarrow = true %} {% extends "./nav.html" %} {% block content %}
<div class="container">
    {% if isLogin %}
    <p>欢迎{{userName}}您已登录，请返回<a href="/">首页</a></p>
    {% else %}
    <form>
        <h1>登录</h1>
        <hr>
        <div class="form-group">
            <label for="input-username">用户名</label>
            <input type="text" class="form-control" id="input-username" placeholder="请输入用户名">

        </div>
        <div class="form-group">
            <label for="input-password">密码</label>
            <input type="password" class="form-control" id="input-password" placeholder="请输入密码">
        </div>
        <button type="submit" class="btn btn-primary login">登录</button>
        <a href="/register">注册账号>></a>
        <a href="/">返回主页>></a>

    </form>
    {% endif %}
</div>


{% endblock %} {% block js %}
<script>
    $(".login").click(function(e) {
        // 阻止默认行为
        e.preventDefault();
        //获取用户名和代码
        const userName = $("#input-username").val();
        const password = $("#input-password").val();
        ajax.post("api/user/login", {
                userName,
                password

            },
            function(err, data) {
                if (err) {
                    alert(err);
                    return;

                }
                alert("登陆成功");
                let search = location.search;

                // 转换成正确的地址
                let redirectUrl = decodeURIComponent(search.split("=")[1]);
                // 重定向
                if (redirectUrl === "undefined") {
                    location.href = "/";
                } else {
                    location.href = redirectUrl;

                }
            })

    })
</script>
{% endblock %}